package com.hub.nio.netty.client;


import com.alibaba.fastjson2.JSONObject;
import com.hub.nio.netty.msg.Message;
import io.netty.util.CharsetUtil;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.Socket;

/**
 * @ author 令狐冲
 * @ date & time 2025/2/5 17:26
 */
public class Client {

    public static void main(String[] args) throws IOException, InterruptedException {
        Socket socket = new Socket("localhost", 18023);

        // 向服务器发消息
        OutputStream outputStream = socket.getOutputStream();

        JSONObject msg = new JSONObject();
        msg.put("msg_type", 1);
        msg.put("body", "你好呀！");

        outputStream.write(msg.toJSONString().getBytes(CharsetUtil.UTF_8));
        outputStream.flush();

        Thread.sleep(10000);
        BufferedReader br = new BufferedReader(new InputStreamReader(socket.getInputStream()));
        char[] ch = new char[65536];
        int len = -1;
        while ((len = br.read(ch)) != -1) {
            String result = new String(ch, 0, len);
            System.out.println("服务器返回数据：" + result);
        }
    }

}
